package com.union.core.sys.service;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.union.core.sys.dao.SysManagerDao;
import com.union.core.sys.dao.SysModuleDao;
import com.union.core.sys.dao.impl.SysManagerDaoImpl;
import com.union.core.sys.dao.impl.SysModuleDaoImpl;
import com.union.core.sys.model.SysManager;
import com.hnjowe.util.ImageCaptchaServiceSingleton;
import com.octo.captcha.service.CaptchaServiceException;

/**
 * 业务逻辑处理类: 管理模块权限/超时判断
 * 用于后台管理系统
 * filename: SysService.java
 * project:  CCTR
 * @company Hainan Jowe Technology Ltd
 * @author wangxiuwu
 * @create in 2009-6-1
 * @version 1.0
 */
public class SysService {
	private static Logger log = Logger.getLogger(SysService.class.getName());
	private SysModuleDao sysModuleDaoImpl;
	private SysManagerDao sysManagerDaoImpl;
	
    private static SysService instance = null;

    private SysService() {
    	sysModuleDaoImpl = new SysModuleDaoImpl();
		sysManagerDaoImpl = new SysManagerDaoImpl();
    }

    /**
     * 返回一个SysService类的单一实例
     * @return m_instance
     */
    public synchronized  static SysService getInstance(){
            if(instance == null){
                    instance = new SysService();
            }
            return instance;
    }
	
	/**
	 * 判断对某模块是否有权限
	 * true: 有权限
	 * false:无权限
	 * @return boolean
	 */
	public boolean ifHasPurview(String moduleName){
		boolean result = false;
		try{
			int moduleId = sysModuleDaoImpl.getModuleId(moduleName);
			result = sysManagerDaoImpl.ifHasModuleMgrPurview(getSessionListener().getMANAGER_NAME(),getSessionListener().getMANAGER_PWD(),moduleId);
		}catch(Exception ex){
			result = false;
			log.error("判断对某模块是否有权限出错:"+ex);
		}
		return result;
	}
	
	/**
	 * 判断登陆系统是否超时
	 * true: 不超时
	 * false:超时
	 * @return boolean
	 */
	public boolean ifTimeOut(){
		boolean result = false;
		try{
			if(null == getSessionListener().getMANAGER_NAME()){
				result = false;//超时
			}else{
				result = true;
			}
		}catch(Exception e){
			result = false;//超时
		}
		return result;
	}
	
	/**
	 * 判断登陆系统是否超时或是否既有权限
	 * @param moduleName String
	 * @return boolean
	 */
	public boolean ifTimeOutOrHasPurview(String moduleName){
		boolean result = false;
		if(getSessionListener().getMANAGER_NAME()==null){
			result = false;//超时
		}else{
			try{
				int moduleId = sysModuleDaoImpl.getModuleId(moduleName);
				result = sysManagerDaoImpl.ifHasModuleMgrPurview(getSessionListener().getMANAGER_NAME(),getSessionListener().getMANAGER_PWD(),moduleId);
			}catch(Exception ex){
				result = false;
				log.error("判断登陆系统是否超时或是否既有权限出错:"+ex);
			}
		}
		return result;
	}
	
	/**
	 * 设置后台系统登陆session信息
	 */
	public void setSessionListener(SysManager sysManager){
		sysManager.setSessionId((String)ServletActionContext.getRequest().getSession().getId());
		ServletActionContext.getRequest().getSession().setAttribute("sysManager",sysManager); 
	}
	
	/**
	 * 获取后台系统登陆session信息
	 */
	public SysManager getSessionListener(){
		return (SysManager)ServletActionContext.getRequest().getSession().getAttribute("sysManager");
	}
	
	/**
	 * 销毁后台系统登陆session信息
	 */
	public void removeSessionListener(){
		try{
			ServletActionContext.getRequest().getSession().removeAttribute("sysManager");
		}catch(Exception e){
		}
	}
	
	/**
	 * 校验图片认证码
	 * @param randCode
	 * @return boolean
	 */ 
	public boolean checkRandCode(String randCode){
		Boolean isResponseCorrect =Boolean.FALSE;
    	String captchaId = (String)ServletActionContext.getRequest().getSession().getId();
    	try {
            isResponseCorrect = ImageCaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, randCode);
        } catch (CaptchaServiceException e) {
        }
        return isResponseCorrect;
	}
	
	
	/**
     * 获得客户端真实IP
     */
	public String getIpAddr(HttpServletRequest request) {    
        String ip = request.getHeader("x-forwarded-for");    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("WL-Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getRemoteAddr();    
        }
        return ip;
    }
	
	
	
}
